import http from "../../utils/http"

Page({
  data: {
    courses: [], // 原始课程数据
    days: ["一", "二", "三", "四", "五"], // 星期数组
    sections: Array(10).fill().map((_, i) => i + 1), // 1-10节课
    gridItems: [] // 课程表网格项
  },

  getTimetable() {
    http({
      url: "/student/timetable",
    }).then(res => {
      console.log(res);
      if(res.code === 0) {
        this.setData({ courses: res.data }, () => {
          this.renderTimetable();
        });
      }
    })
  },

  // 渲染课程表
  renderTimetable() {
    const gridItems = [];
    const dayWidth = 150; // 每天宽度(rpx)
    const sectionHeight = 120; // 每节课高度(rpx)
    
    this.data.courses.forEach(course => {
      const [day, start, duration] = course.time.split('-').map(Number);
      
      if (day >= 1 && day <= 5 && start >= 1 && start <= 10) {
        const dayIndex = day - 1;
        const startIndex = start - 1;
        
        gridItems.push({
          ...course,
          top: startIndex * sectionHeight,
          height: duration * sectionHeight,
          left: (dayIndex + 1) * dayWidth, // +1 为节数列留空间
          width: dayWidth,
          start,
          end: start + duration - 1
        });
      }
    });
    
    this.setData({ gridItems });
  },

  onShow() {
    this.getTimetable();
  }
})